<?php
/* ------------------------------------------------------------------------
 * AION-KOREA Admin Control Panel
 *
 * @version 1.0
 * @author Taro6222 (AION-KOREA Admin CP main Developer)
 * @link http://redmine.aionkorea.org/projects/ak-admin-cp
 * @license http://creativecommons.org/licenses/by-nc-sa/3.0/deed.ko
 * @APM http://www.apachefriends.org/en/xampp.html
 *
------------------------------------------------------------------------ */

class daoInstall
{
	/**
	 * Stores the instance of this class.
	 *
	 * @var -
	 */
	private static $instance = null;
	
	
	/**
	 * Returns the instance of the class.
	 *
	 * @return instance
	 */
	public static function getInstance()
	{
	    if ( is_null( self::$instance ) )
	    {
	        self::$instance = new self();
	    }
	    return self::$instance;
	}


	/**
	 * 데이터베이스 접속 테스트
	 */
	public static function dbConnect()
	{
		try
		{
			extract( $_POST );

			$parameters = array(
			    'host'           => $db_host,
			    'username'       => $db_user,
			    'password'       => $db_password,
			    'dbname'         => $db_login,
			);

			$db = Zend_Db::factory('Pdo_Mysql', $parameters);
			$db->getConnection();

			$parameters = array(
			    'host'           => $db_host,
			    'username'       => $db_user,
			    'password'       => $db_password,
				'dbname'         => $db_game
			);

			$db = Zend_Db::factory('Pdo_Mysql', $parameters);
			$db->getConnection();

			$ret = true;
		}
		catch( Zend_Db_Adapter_Exception $e )
		{
			$ret = false;
		}

		return $ret;
	}


	public static function install()
	{
		//설치 도중 도망가는 것을 방지?
		$_SESSION['install_session_type'] = 1;

		extract( $_POST );
		$returnMessage = '알 수 없는 오류가 발생하였습니다.';

		if ( !daoInstall::dbConnect() ) {
			$returnMessage = '데이터베이스 접속 오류!<br><br>DB정보를 확인해 주세요.';
		}
		else if ( !function_exists( 'simplexml_load_file' ) ) {
			$returnMessage = 'SimpleXML 라이브러리를 설치하세요 !';
		}
		else if ( !function_exists( 'sqlite_open' ) ) {
			$returnMessage = 'SQLite 라이브러리를 설치하세요!';
		}
		else {
			$contents = "<?php\r\n\$db_host='".$db_host."';\r\n\$db_user='".$db_user."';\r\n\$db_password='".$db_password."';\r\n\$db_login_server='".$db_login."';\r\n\$db_game_server='".$db_game."';\r\n\$use=0;\r\n";
			file_put_contents( __GLOBAL_CONFIG_FILE, $contents );

			if ( !is_writable( __GLOBAL_CONFIG_FILE ) && file_exists( __GLOBAL_CONFIG_FILE ) ) {
				$returnMessage = "파일을 기록할 수 없습니다.";
			}
			else {
				include_once 'library/dao.user.php';

				if ( daoUser::checkUserId( $account ) ) {
					if ( !daoUser::checkUserPassword( $account, $password ) ) {
						$returnMessage = '계정이 존재하지 않거나 비밀번호가 틀립니다.';
					}
					else {
						$db = common::dbConnections(1);

						$data = array(
							'access_level' => 5
						);
						$where['name = ?'] = $account;
						$db->update('account_data', $data, $where);
						unset($where);

						//session_unregister( $_SESSION['install_session_type'] );
						session_unset();
						session_destroy();

						$returnMessage = 'OK';
					}
				}
				else {
					$db = common::dbConnections(1);

					$account = trim( $account );
					$password = base64_encode( sha1( strtolower( trim( $password ) ), true ) );
					$data = array(
						'name' => $account,
						'password' => $password,
						'access_level' => 5,
					);

					$db->insert('account_data', $data);

					//session_unregister( $_SESSION['install_session_type'] );
					session_unset();
					session_destroy();

					$returnMessage = 'OK';
				}
			}
		}

		return $returnMessage;
	}

}
?>
